package com.moxiao.yfjh.module.activity.service.packageType;

import com.moxiao.yfjh.module.activity.dal.dataobject.productInfo.ProductInfoDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.activity.controller.admin.packageType.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.packageType.PackageTypeDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.activity.convert.packageType.PackageTypeConvert;
import com.moxiao.yfjh.module.activity.dal.mysql.packageType.PackageTypeMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲套餐类型 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class PackageTypeServiceImpl implements PackageTypeService {

    @Resource
    private PackageTypeMapper packageTypeMapper;

    @Override
    public Long createPackageType(PackageTypeCreateReqVO createReqVO) {
        // 插入
        PackageTypeDO packageType = PackageTypeConvert.INSTANCE.convert(createReqVO);
        packageTypeMapper.insert(packageType);
        // 返回
        return packageType.getId();
    }

    @Override
    public void updatePackageType(PackageTypeUpdateReqVO updateReqVO) {
        // 校验存在
        validatePackageTypeExists(updateReqVO.getId());
        // 更新
        PackageTypeDO updateObj = PackageTypeConvert.INSTANCE.convert(updateReqVO);
        packageTypeMapper.updateById(updateObj);
    }

    @Override
    public void deletePackageType(Long id) {
        // 校验存在
        validatePackageTypeExists(id);
        // 删除
        packageTypeMapper.deleteById(id);
    }

    private void validatePackageTypeExists(Long id) {
        if (packageTypeMapper.selectById(id) == null) {
            throw exception(PACKAGE_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public PackageTypeDO getPackageType(Long id) {
        return packageTypeMapper.selectById(id);
    }

    @Override
    public List<PackageTypeDO> getPackageTypeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return packageTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PackageTypeDO> getPackageTypePage(PackageTypePageReqVO pageReqVO) {
        return packageTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PackageTypeDO> getPackageTypeList(PackageTypeExportReqVO exportReqVO) {
        return packageTypeMapper.selectList(exportReqVO);
    }

    @Override
    public PackageTypeDO getPackageTypeTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<PackageTypeDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(PackageTypeDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(PackageTypeDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return packageTypeMapper.selectOne(lqw);
    }

    @Override
    public Integer getPackageTypeNo(String packageTypeNo) {
        return packageTypeMapper.getPackageTypeNo(packageTypeNo);
    }

}
